package com.topsec.manager.timer;

import com.luciad.imageio.webp.WebPReadParam;
import com.topsec.manager.controller.req.ToolReq;
import com.topsec.manager.dao.ReleaseMapper;
import com.topsec.manager.entity.ToolBo;
import com.topsec.manager.util.ImageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.FileImageInputStream;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;

/**
 * .
 * <p>
 * -----------------------------------------------------------------------------
 * <p>
 * 工程名 ： topsec-manager
 * <p>
 *
 * @author 赵艳生
 * @lastMonify 2018年3月9日
 * @since JDK1.8
 */
public class FileMoniterTimer {
	public static void main(String[] args) {
		long currentTimeMillis = System.currentTimeMillis();
		System.out.println(currentTimeMillis);
		String valueOf = String.valueOf(currentTimeMillis);
		System.out.println(valueOf.substring(5, valueOf.length()));

	}

	// 日志记录器
	private final Logger log = LoggerFactory.getLogger(this.getClass());
	private String RZX = "/home/tool/rzx";
	private String BY = "/home/tool/by";
	private String JSS = "/home/tool/jss";
	private String FZ = "/home/tool/fz";
	// private String TOMCATPATH =
	// "/workspacetopsec/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/topsec-web/WEB-INF/classes/static/images/tool/";
	private String TOMCATPATH = "/topsec/web/tomcat/webapps/topsec-web/WEB-INF/classes/static/images/tool/";
	// private String TOMCATPATH = "/home/";
	// 日志记录器
	@Autowired
	private ReleaseMapper releaseMapper;

	public void autoSmsTime() {
		try {
			List<ToolBo> tlist = releaseMapper.findtool();
			for (int i = 0; i < tlist.size(); i++) {
				ToolBo t = tlist.get(i);
				String icopath = t.getIcoshow();
				if (icopath != null && !"".equals(icopath)) {
					if (icopath.indexOf("../") == -1) {
						String sfile = "";
						String efile = "";
						String suffix = "";
						String fn = "../../images/tool/";
						String newfilename = "";
						long currentTimeMillis = System.currentTimeMillis();
						String[] split = icopath.split("\\.");
						if (split.length > 0) {
							suffix = split[split.length - 1];
							if ("1".equals(t.getDeveloper())) {
								sfile = BY + icopath;
							} else if ("2".equals(t.getDeveloper())) {
								sfile = RZX + icopath;
							} else if ("3".equals(t.getDeveloper())) {
								sfile = JSS + icopath;
							} else if ("4".equals(t.getDeveloper())) {
								sfile = FZ + icopath;
							}
							newfilename = currentTimeMillis + "." + suffix;
							fn = fn + newfilename;
							efile = TOMCATPATH + newfilename;
							File file = new File(sfile);
							if (file.isFile()) {
								copyFile(file, new File(efile), suffix);
								ToolReq tq = new ToolReq();
								tq.setIcoshow(fn);
								tq.setToolid(String.valueOf(t.getId()));
								releaseMapper.updatToolIcoshow(tq);
							}
						}
					}
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * copy file
	 *
	 * @param sourceFile
	 * @param targetFile
	 */
	private static void copyFile(File sourceFile, File targetFile, String suffix) {

		FileInputStream input = null;
		BufferedInputStream inBuff = null;
		FileOutputStream output = null;
		BufferedOutputStream outBuff = null;

		FileImageInputStream imageInput = null;
		try {
			imageInput = new FileImageInputStream(sourceFile);
			String type = ImageUtil.getImageType(imageInput);

			if ("webp".equals(type)) { // 解码webp为常规格式图片数据
				// 获取实例
				ImageReader reader = ImageIO.getImageReadersByMIMEType("image/webp").next();
				// 配置解码器
				WebPReadParam readParam = new WebPReadParam();
				readParam.setBypassFiltering(true);
				// 读取文件
				reader.setInput(imageInput);
				// 解码图像
				BufferedImage image = reader.read(0, readParam);
				ImageIO.write(image, suffix, targetFile);
				// 关闭
				reader.dispose();
			} else {
				input = new FileInputStream(sourceFile);
				inBuff = new BufferedInputStream(input);
				output = new FileOutputStream(targetFile);
				outBuff = new BufferedOutputStream(output);
				byte[] b = new byte[1024 * 5];
				int len;
				while ((len = inBuff.read(b)) != -1) {
					outBuff.write(b, 0, len);
				}
				outBuff.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (imageInput != null) {
				try {
					// 关闭输入流
					imageInput.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (inBuff != null) {
				try {
					// 关闭输入流
					inBuff.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (outBuff != null) {
				try {
					// 关闭输出流
					outBuff.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
